package com.yxq.springboot.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yxq.springboot.bean.SpringBootUser;
import com.yxq.springboot.bean.SpringBootUserLog;
import com.yxq.springboot.dao.SpringBootUserDao;
import com.yxq.springboot.dao.SpringBootUserLogDao;

@Service
public class UserService {

	@Autowired
	private SpringBootUserDao springbootUserDao;

	@Autowired
	private SpringBootUserLogDao SpringBootUserLogDao;

	/**
	 * 用户注册
	 * 
	 * @return	
	 */
	@Transactional
	public String register(String name, String ip) {
		// 1.添加用户
		SpringBootUser springbootUser = new SpringBootUser();
		springbootUser.setName(name);
		springbootUser.setCreateTime(new Date());
		springbootUserDao.insert(springbootUser);
		
		// 测试使用
		//boolean flag = true;
		//if (flag) {
		//	throw new RuntimeException();
		//}

		// 2.添加注册日志
		SpringBootUserLog SpringBootUserLog = new SpringBootUserLog();
		SpringBootUserLog.setUserName(name);
		SpringBootUserLog.setUserIp(ip);
		SpringBootUserLog.setCreateTime(new Date());
		SpringBootUserLogDao.save(SpringBootUserLog);

		return "success";
	}

}
